package org.bonorsoft.modules.wechat.controller;

import cn.hutool.core.map.MapUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.modules.control.entity.Address;
import org.bonorsoft.modules.control.entity.Hazard;
import org.bonorsoft.modules.control.service.IAddressService;
import org.bonorsoft.modules.control.service.IInfoTemplateService;
import org.bonorsoft.modules.control.service.ITemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/wechat/check")
public class WxTemplateController {

    @Autowired
    private IAddressService addressService;

    @Autowired
    private ITemplateService templateService;

    @Autowired
    private IInfoTemplateService infoTemplateService;

    /**
     * @param tId 模板id
     * @param bId 基础地址id
     * @return
     */
    @GetMapping("/getTemplate")
    @RequiresPermissions("control:address:check")
    public Map getTemplate(String tId, String bId) {
        List<Hazard> hazardList = infoTemplateService.getHazardNameByDzbm(bId);
        StringJoiner hazardNames = new StringJoiner(",");
        StringJoiner hazardIds = new StringJoiner(",");
        hazardList.forEach(item -> {
            hazardIds.add(item.getHazardId().toString());
            hazardNames.add(item.getHazardName());
        });
        Address address = addressService.getById(bId);
        return MapUtil.builder()
                .put("template", templateService.getById(tId).getTemplateHtml())
                .put("checkAddress", address.getDzmc())
                .put("addressId", address.getDzbm())
                .put("hazardIds", hazardIds.toString())
                .put("hazardNames", hazardNames.toString())
                .build();
    }
}
